package game.game.testing;

import com.crunch.math.Vector2f;
import com.crunch.physics.DynamicPhysicsEntity;
import com.crunch.physics.MovablePhysicsEntity;
import com.crunch.physics.Polygon;
import game.game.GameActor;
import org.json.JSONException;
import org.json.JSONObject;

import java.awt.event.KeyEvent;

/**
 * A moving platform object.
 */
public class TestMovingPlatform extends GameActor {
	public void initState(JSONObject state)
			throws JSONException {
		// create a polygon for the shape
		Vector2f[] vertices = new Vector2f[] {
				new Vector2f(1.0f, 1.0f),
				new Vector2f(-1.0f, 1.0f),
				new Vector2f(-1.0f, -1.0f),
				new Vector2f(1.0f, -1.0f)
		};
		com.crunch.physics.Polygon polygon = new Polygon(vertices);
		MovablePhysicsEntity.Description desc = new MovablePhysicsEntity.Description();

		desc.shape = polygon;
		desc.rotation = 0.0f;
		desc.initialPosition = new Vector2f(3.0f, 3.0f);
		desc.initialVelocity = new Vector2f(0.0f, 0.0f);
		desc.maxSquishAngle = (float) Math.PI * 0.25f;
		desc.data = this;
		desc.eventHandler = new MovablePhysicsEntity.EventHandler() {
			@Override
			public void onSquish(MovablePhysicsEntity movableEntity, DynamicPhysicsEntity dynamicEntity) {
				System.out.println(movableEntity + " squished " + dynamicEntity);
			}
		};

		entity = getRoomState().getPhysicsSimulation().addMovableEntity(desc);
	}

	public void onGameActorDestroy() {
		if (entity != null) {
			entity.remove();
			entity = null;
		}
	}

	public void onUpdate() {
		Vector2f vel = new Vector2f();
		vel.x((getEngine().input.isKeyDown(KeyEvent.VK_NUMPAD4) ? -1.0f : 0.0f) +
				(getEngine().input.isKeyDown(KeyEvent.VK_NUMPAD6) ? 1.0f : 0.0f));
		vel.y((getEngine().input.isKeyDown(KeyEvent.VK_NUMPAD2) ? -1.0f : 0.0f) +
				(getEngine().input.isKeyDown(KeyEvent.VK_NUMPAD8) ? 1.0f : 0.0f));
		entity.setVelocity(vel);
	}

	public void loadState(JSONObject state)
			throws JSONException {
		// ...
	}

	public JSONObject saveState()
			throws JSONException {
		// ...
		return null;
	}

	private MovablePhysicsEntity entity;
}
